﻿Imports Microsoft.VisualBasic
Imports Microsoft.VisualBasic.CompilerServices
Imports System
Imports System.Drawing
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Text

Namespace Stickam_Client
    <StandardModule> _
    Friend NotInheritable Class Globals
        ' Methods
        Public Shared Function AddSplitters(ByVal Data As String, ByVal UnknownValue As Integer) As String
            Dim str2 As String = ""
            If (Data.Length > &H80) Then
                Dim ch As Char = Strings.Chr((&HC0 + UnknownValue))
                Dim length As Integer = Data.Length
                Dim i As Integer = 1
                Do While (i <= length)
                    Dim str3 As String = Strings.Mid(Data, i, &H80)
                    If (str3.Length = &H80) Then
                        str3 = (str3 & Conversions.ToString(ch))
                    End If
                    str2 = (str2 & str3)
                    i = (i + &H80)
                Loop
                Return str2
            End If
            Return Data
        End Function

        Public Shared Function BytesToInt(ByVal ByteStr As String) As Integer
            Dim num2 As Integer
            Dim bytes As Byte() = Globals.Encoder.GetBytes(Strings.StrReverse(ByteStr))
            Dim handle As GCHandle = GCHandle.Alloc(num2, GCHandleType.Pinned)
            Dim destination As IntPtr = handle.AddrOfPinnedObject
            Marshal.Copy(bytes, 0, destination, bytes.Length)
            num2 = Marshal.ReadInt32(destination)
            handle.Free
            Return num2
        End Function

        Public Shared Function CleanSplitters(ByVal Data As String, ByVal Stepper As Integer) As String
            If (Stepper = &H85) Then
                Data = Strings.Mid(Data, 5)
            End If
            Dim str2 As String = ""
            Dim num3 As Integer = Stepper
            Dim length As Integer = Data.Length
            Dim i As Integer = 1
            Do While (((num3 >> &H1F) Xor i) <= ((num3 >> &H1F) Xor length))
                str2 = (str2 & Strings.Mid(Data, i, &H80))
                i = (i + num3)
            Loop
            Return str2
        End Function

        Public Shared Function ColorFromString(ByVal ColorStr As String) As Color
            If (ColorStr = "Default") Then
                Return Globals.DefaultColor
            End If
            If (Globals.IsHex(ColorStr) And (ColorStr.Length = 6)) Then
                Return Color.FromArgb(Conversions.ToInteger((Globals.H & Strings.Mid(ColorStr, 1, 2))), Conversions.ToInteger((Globals.H & Strings.Mid(ColorStr, 3, 2))), Conversions.ToInteger((Globals.H & Strings.Mid(ColorStr, 5, 2))))
            End If
            Return Color.FromName(ColorStr)
        End Function

        Public Shared Function ColorToHex(ByVal ColorObj As Color) As String
            Return (Globals.FixedHex(ColorObj.R) & Globals.FixedHex(ColorObj.G) & Globals.FixedHex(ColorObj.B))
        End Function

        Private Shared Function FixedHex(ByVal IntVal As Integer) As String
            Dim str2 As String = Conversion.Hex(IntVal)
            Return (Strings.Mid(Globals.B, 1, (2 - str2.Length)) & str2)
        End Function

        Public Shared Function GetParam(ByVal ParamName As String, ByVal Data As String, ByVal Ender As String) As String
            ParamName = (ParamName & "=")
            Dim str As String = ""
            Dim num As Integer = Strings.InStr(Data, ParamName, CompareMethod.Binary)
            If (num > 0) Then
                str = Strings.Mid(Data, (num + ParamName.Length))
                str = Strings.Mid(str, 1, (Strings.InStr(str, Ender, CompareMethod.Binary) - 1))
            End If
            Return str
        End Function

        Public Shared Function HttpGet(ByVal HostName As String, ByVal FilePath As String, ByVal Cookie As String) As String
            Dim str As String = ""
            If (Cookie.Length > 0) Then
                str = ("Cookie: " & Cookie & ChrW(13) & ChrW(10))
            End If
            Return String.Concat(New String() { "GET ", FilePath, " HTTP/1.1" & ChrW(13) & ChrW(10), str, "Host: ", HostName, ChrW(13) & ChrW(10) & "Connection: Close" & ChrW(13) & ChrW(10) & ChrW(13) & ChrW(10) })
        End Function

        Public Shared Function IntToBytes(ByVal IntValue As Integer, ByVal BytesTotal As Integer) As String
            Dim destination As Byte() = New Byte(((BytesTotal - 1) + 1)  - 1) {}
            Dim handle As GCHandle = GCHandle.Alloc(IntValue, GCHandleType.Pinned)
            Marshal.Copy(handle.AddrOfPinnedObject, destination, 0, BytesTotal)
            handle.Free
            Return Strings.StrReverse(Globals.Encoder.GetString(destination))
        End Function

        Public Shared Function IsHex(ByVal HexStr As String) As Boolean
            HexStr = HexStr.ToUpper
            Dim length As Integer = HexStr.Length
            Dim i As Integer = 1
            Do While (i <= length)
                If (Strings.InStr(Globals.HexChars, Strings.Mid(HexStr, i, 1), CompareMethod.Binary) = 0) Then
                    Return False
                End If
                i += 1
            Loop
            Return True
        End Function

        Public Shared Function IsValidIP(ByVal IP As String) As Boolean
            Dim flag2 As Boolean
            Dim num5 As Integer
            Try 
                Dim num6 As Integer
            Label_0000:
                ProjectData.ClearProjectError
                Dim num4 As Integer = -2
            Label_0009:
                num6 = 2
                Dim flag As Boolean = True
            Label_000E:
                num6 = 3
                Dim strArray As String() = Strings.Split(IP, ":", -1, CompareMethod.Binary)
            Label_001F:
                num6 = 4
                If (strArray.Length <> 2) Then
                    goto Label_00CF
                End If
            Label_002B:
                num6 = 5
                Dim num As Integer = 0
            Label_0030:
                num6 = 6
                num = Conversions.ToInteger(strArray(1))
            Label_003C:
                num6 = 7
                If Not ((num > 0) And (num < &H10000)) Then
                    goto Label_00C3
                End If
            Label_004E:
                num6 = 8
                Dim strArray2 As String() = Strings.Split(strArray(0), ".", -1, CompareMethod.Binary)
            Label_0062:
                num6 = 9
                If (strArray2.Length <> 4) Then
                    goto Label_00B7
                End If
            Label_006D:
                num6 = 10
                Dim index As Integer = 0
            Label_0074:
                num6 = 11
                Dim num2 As Integer = -1
            Label_007B:
                num6 = 12
                num2 = Conversions.ToInteger(strArray2(index))
            Label_008B:
                num6 = 13
                If Not ((num2 < 0) Or (num2 > &HFF)) Then
                    goto Label_00A6
                End If
            Label_00A0:
                num6 = 14
                flag = False
            Label_00A6:
                num6 = &H10
                index += 1
                If (index <= 3) Then
                    goto Label_0074
                End If
                goto Label_00D9
            Label_00B7:
                num6 = &H12
            Label_00BB:
                num6 = &H13
                flag = False
                goto Label_00D9
            Label_00C3:
                num6 = &H16
            Label_00C7:
                num6 = &H17
                flag = False
                goto Label_00D9
            Label_00CF:
                num6 = &H1A
            Label_00D3:
                num6 = &H1B
                flag = False
            Label_00D9:
                num6 = &H1D
                flag2 = flag
                goto Label_01BE
            Label_00E9:
                num5 = 0
                Select Case (num5 + 1)
                    Case 1
                        goto Label_0000
                    Case 2
                        goto Label_0009
                    Case 3
                        goto Label_000E
                    Case 4
                        goto Label_001F
                    Case 5
                        goto Label_002B
                    Case 6
                        goto Label_0030
                    Case 7
                        goto Label_003C
                    Case 8
                        goto Label_004E
                    Case 9
                        goto Label_0062
                    Case 10
                        goto Label_006D
                    Case 11
                        goto Label_0074
                    Case 12
                        goto Label_007B
                    Case 13
                        goto Label_008B
                    Case 14
                        goto Label_00A0
                    Case 15, &H10
                        goto Label_00A6
                    Case &H11, 20, &H15, &H18, &H19, &H1C, &H1D
                        goto Label_00D9
                    Case &H12
                        goto Label_00B7
                    Case &H13
                        goto Label_00BB
                    Case &H16
                        goto Label_00C3
                    Case &H17
                        goto Label_00C7
                    Case &H1A
                        goto Label_00CF
                    Case &H1B
                        goto Label_00D3
                    Case 30
                        goto Label_01BE
                    Case Else
                        goto Label_01B3
                End Select
            Label_0173:
                num5 = num6
                Select Case If((num4 > -2), num4, 1)
                    Case 0
                        goto Label_01B3
                    Case 1
                        goto Label_00E9
                End Select
            Catch obj1 As Object When (?)
                ProjectData.SetProjectError(DirectCast(obj1, Exception))
                goto Label_0173
            End Try
        Label_01B3:
            Throw ProjectData.CreateProjectError(-2146828237)
        Label_01BE:
            If (num5 <> 0) Then
                ProjectData.ClearProjectError
            End If
            Return flag2
        End Function

        Public Shared Function LoadFile(ByVal FileName As String) As String
            Dim str As String = ""
            If File.Exists(FileName) Then
                Dim stream As FileStream = File.OpenRead(FileName)
                Dim array As Byte() = New Byte((CInt((stream.Length - 1)) + 1)  - 1) {}
                stream.Read(array, 0, CInt(stream.Length))
                stream.Close
                str = Globals.Encoder.GetString(array)
            End If
            Return str
        End Function

        Public Shared Function RandomNumber(ByVal Min As Integer, ByVal Max As Integer) As Object
            VBMath.Randomize
            Return Conversion.Int(CSng(((((Max - Min) + 1) * VBMath.Rnd) + Min)))
        End Function

        Public Shared Function RndByte() As String
            Globals.Randomizer = Conversions.ToInteger(Operators.AddObject(Globals.Randomizer, Globals.RandomNumber(1, &HFF)))
            Globals.Randomizer = (Globals.Randomizer Mod &HFF)
            If (Globals.Randomizer = 0) Then
                Globals.Randomizer += 1
            End If
            Return Conversions.ToString(Strings.Chr(Globals.Randomizer))
        End Function

        Public Shared Function RtmpHandshake() As String
            Dim str As String = String.Concat(New String() { ChrW(3) & ChrW(1), Globals.RndByte, Globals.RndByte, Globals.RndByte, ChrW(9) & ChrW(0) & "|" & ChrW(2) })
            Dim num As Integer = 1
            Do
                str = (str & Globals.RndByte)
                num += 1
            Loop While (num <= &H5F8)
            Return str
        End Function

        Public Shared Sub SaveFile(ByVal FileName As String, ByVal Data As String)
            Dim stream As FileStream = File.Open(FileName, FileMode.Create)
            stream.Write(Globals.Encoder.GetBytes(Data), 0, Data.Length)
            stream.Close
        End Sub


        ' Fields
        Private Shared B As String = "00"
        Public Shared DefaultColor As Color = Color.FromArgb(&HC0, &HC0, &HC0)
        Public Shared Encoder As Encoding = Encoding.Default
        Public Shared EncoderUTF8 As Encoding = Encoding.UTF8
        Private Shared H As String = "&H"
        Private Shared HexChars As String = "0123456789ABCDEF"
        Private Shared Randomizer As Integer = 0
    End Class
End Namespace

